
/***
 * Copyright (c) 2012 Centrodata Sistemas e Automação - www.centrodata.com.br
 * All rights reserved.
 */

package br.com.centrodata.dao;

import br.com.caelum.vraptor.ioc.Component;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author João Fernandes Lima Neto
 */
@Component
public class DefaultGenericDao<T> implements GenericDao<T> {

    private Session session;
    private Class   persistentClass;    

    public DefaultGenericDao(Session session) {
        this.session         = session;
        
        Class<T> clazz = (Class<T>) ((ParameterizedType) getClass()  
                .getGenericSuperclass()).getActualTypeArguments()[0];  
        
        this.persistentClass = clazz;  
    }    
    
    
    @Override
    public void add(T object) {
        this.session.save(object);
    }

    @Override
    public void update(T object) {
        this.session.update(object);
    }

    @Override
    public void refresh(T object) {
        this.session.refresh(object);
    }

    @Override
    public void del(T object) {
        this.session.delete(object);
    }

    @Override
    public T search(int id) {
        return (T) this.session.load(persistentClass, id);
    }

    @Override
    public List<T> search() {
        return (List<T>) this.session.createCriteria(persistentClass).list();
    }

    @Override
    public Session getSession(){
        return this.session;
    }
    
}
